package com.steven.ourcontest.forum.view

import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.steven.ourcontest.R
import com.steven.ourcontest.app.AppRouter
import com.steven.ourcontest.base.BaseFragment
import com.steven.ourcontest.login.helper.LoginController
import com.steven.ourcontest.utils.toast
import kotlinx.android.synthetic.main.fragment_forum.*

class ForumFragment : BaseFragment() {

    override fun getViewId(): Int {
        return R.layout.fragment_forum
    }

    private val adapter by lazy {
        AdapterFragmentPager(this)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
    }

    private fun initView() {
        tv_hot?.textSize = 18f
        tv_new?.textSize = 14f
        iv_new?.isVisible = false
        ll_hot?.setOnClickListener {
            tv_hot?.textSize = 18f
            tv_new?.textSize = 14f
            iv_new?.isVisible = false
            iv_hot?.isVisible = true
            view_pager_content?.setCurrentItem(0, true)
        }
        ll_new?.setOnClickListener {
            tv_new?.textSize = 18f
            tv_hot?.textSize = 14f
            iv_hot?.isVisible = false
            iv_new?.isVisible = true
            view_pager_content?.setCurrentItem(1, true)
        }
        view_pager_content?.adapter = adapter
        view_pager_content?.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                when (position) {
                    0 -> {
                        tv_hot?.textSize = 18f
                        tv_new?.textSize = 14f
                        iv_new?.isVisible = false
                        iv_hot?.isVisible = true
                    }
                    1 -> {
                        tv_new?.textSize = 18f
                        tv_hot?.textSize = 14f
                        iv_hot?.isVisible = false
                        iv_new?.isVisible = true
                    }
                }
            }
        })

        iv_write?.setOnClickListener {
            if (!LoginController.isLogin) {
                toast("请先登录")
                return@setOnClickListener
            }
            if (LoginController.userType == "") {
                toast("该账户尚未通过身份认证，如已提交请耐心等待~")
                return@setOnClickListener
            }
            context?.let { it1 -> AppRouter.startWriteForumActivity(it1) }
        }
    }
}

class AdapterFragmentPager(fragment: Fragment) : FragmentStateAdapter(fragment) {

    val fragments: ArrayList<BaseFragment> = ArrayList()

    init {
        fragments.add(HotForumFragment())
        fragments.add(NewForumFragment())
    }

    override fun createFragment(position: Int): Fragment {
        return fragments[position]
    }

    override fun getItemCount(): Int {
        return fragments.size
    }

    fun <T : BaseFragment?> get(t: Class<T>): T? {
        for (fragment in fragments) {
            if (t.isInstance(fragment)) {
                return t.cast(fragment)
            }
        }
        return null
    }
}